package com.example.sherlockzhong.plush_v2.Subscriptions;

/*
 * Created by DosonLeung on 2014/8/30.
 */

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.Components.buttons.BackButton;
import com.example.sherlockzhong.plush_v2.Models.PushModel;
import com.example.sherlockzhong.plush_v2.R;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ClassActivity extends Activity {
    JSONArray classArray;
    String[] itemName = {"title", "time", "content"};
    int Id[] = {R.id.class_title, R.id.class_time, R.id.class_content};
    ArrayList<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
    ArrayList<String> classId = new ArrayList<String>();
    JSONArray DataArray;
    ImageView class_refresh;
    JSONObject DataObject;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //0x123表示请求班级的Id完毕
            if (msg.what == 0x123) {
                if (classArray != null) {
                    //获得班级的ID数组
                    getClassId(classArray);
                    new Thread() {
                        @Override
                        public void run() {
                            DataArray = PushModel.getPosterNews(classId.get(0));
                            handler.sendEmptyMessage(0x200);
                        }
                    }.start();
                } else {
                    //不能得到班级的ID
                    ClassActivity.this.stopReflashDialog();
                }
            }//0x200表示请求指定ID的班级信息完毕
            else if (msg.what == 0x200) {
                ClassActivity.this.stopReflashDialog();
                //获取指定Id的班级信息成功！
                if (DataArray != null) {
                    itemList = new ArrayList<Map<String, Object>>();
                    Map<String, Object> item;
                    int length = DataArray.length();
                    try {
                        for (int i = 0; i < length; i++) {
                            item = new HashMap<String, Object>();
                            DataObject = DataArray.getJSONObject(i);
                            item.put("title", DataObject.getString("title"));
                            item.put("time", DataObject.getString("create_time"));
                            item.put("content", DataObject.getString("content"));
                            itemList.add(item);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    SimpleAdapter SA = new SimpleAdapter(ClassActivity.this, itemList, R.layout.class_item, itemName, Id);
                    ((ListView) findViewById(R.id.class_list)).setAdapter(SA);
                    //获取指定Id的班级信息失败
                } else
                    Toast.makeText(ClassActivity.this, "网络超时", Toast.LENGTH_SHORT).show();
            } else {
                ClassActivity.this.stopReflashDialog();
                Toast.makeText(ClassActivity.this, "网络超时", Toast.LENGTH_SHORT).show();
            }
        }
    };
    private ProgressDialog pd;
    Thread getClassThread = new Thread() {
        @Override
        public void run() {
            classArray = PushModel.listAllPosters("2");
            handler.sendEmptyMessage(0x123);
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class_list);
        ((BackButton) findViewById(R.id.class_back)).setApp(this);
        initWidget();
        this.showReflashDialog("加载中...");
        getClassThread.start();
    }

    private void getClassId(JSONArray classArray) {
        int Length = classArray.length();
        JSONObject classObject;
        try {
            for (int i = 0; i < Length; i++) {
                classObject = classArray.getJSONObject(i);
                classId.add(classObject.getString("id"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    /**
     * 显示刷新的时候的等待进度条
     */
    private void showReflashDialog(String msg) {
        this.pd = new ProgressDialog(this);
        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.pd.setCancelable(true);
        this.pd.setMessage(msg);
        this.pd.show();
    }

    /**
     * 结束刷新的时候的等待进度条
     */
    private void stopReflashDialog() {
        if (this.pd != null) {
            pd.dismiss();
        }
    }

    public void onDestory() {
        super.onDestroy();
        stopReflashDialog();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            stopReflashDialog();
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initWidget(){
        class_refresh = (ImageView)findViewById(R.id.class_refresh);
        class_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimationSet as = new AnimationSet(true);
                final float w = class_refresh.getWidth() / 2;
                final float h = class_refresh.getHeight() / 2;
                RotateAnimation ra = new RotateAnimation(0, 720, w, h);
                ra.setDuration(500);
                as.addAnimation(ra);
                class_refresh.startAnimation(as);
                showReflashDialog("加载中...");
                getNewThread().start();
            }
        });
    }
    
    private Thread getNewThread(){
        Thread getClassThread = new Thread() {
            @Override
            public void run() {
                classArray = PushModel.listAllPosters("2");
                handler.sendEmptyMessage(0x123);
            }
        };
        return getClassThread;
    }

}
